package pri.tool.anv4l2camera;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import pri.tool.v4l2camera.IDataCallback;
import pri.tool.v4l2camera.IStateCallback;
import pri.tool.v4l2camera.Size;
import pri.tool.v4l2camera.V4L2Camera;

public class V4L2CameraActivity extends Activity {
    private final static String TAG = "V4L2CameraActivity";

    private V4L2Camera v4l2Camera;
    private V4L2Camera v4l2Camera2;

    private AutoFitSurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

    private AutoFitSurfaceView mSurfaceView2;
    private SurfaceHolder mSurfaceHolder2;

    private static final int PREVIEW_WIDTH = 640;
    private static final int PREVIEW_HEIGHT = 480;

    private List<String> videoList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        File devDir = new File("/dev");
        if(devDir.exists()
                && devDir.isDirectory()
                && devDir.listFiles()!=null
                && devDir.listFiles().length>0){
            for(File item : devDir.listFiles()){
                if(item.getName().contains("video") && !item.getName().contains("video_state")){
                    String path = item.getAbsolutePath();
                    Log.d(TAG, "path: "+path);
                    videoList.add(path);
                }
            }
        }

        initView();
    }

    public void initView() {
        mSurfaceView = findViewById(R.id.cameraSurface);
        mSurfaceView2 = findViewById(R.id.cameraSurface2);

        if(videoList.size()>0){
            mSurfaceHolder = mSurfaceView.getHolder();
            mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {
                    Log.e(TAG, "Surface create");
                    v4l2Camera = initCamera(videoList.get(0), mSurfaceView, mSurfaceHolder);
                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) { }
            });
            if(videoList.size()>1){
                mSurfaceHolder2 = mSurfaceView2.getHolder();
                mSurfaceHolder2.addCallback(new SurfaceHolder.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceHolder surfaceHolder) {
                        Log.e(TAG, "Surface create");
                        v4l2Camera2 = initCamera(videoList.get(1), mSurfaceView2, mSurfaceHolder2);
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder surfaceHolder) { }
                });
            }
        }

    }



    public V4L2Camera initCamera(String videoFilePath, AutoFitSurfaceView surfaceView, SurfaceHolder surfaceHolder) {
        V4L2Camera adCamera = new V4L2Camera();
        adCamera.init(new CameraStateCallback(adCamera, surfaceView, surfaceHolder), this);
        adCamera.open(videoFilePath);
        return adCamera;
    }

    class CameraStateCallback implements IStateCallback {
        V4L2Camera adCamera;
        private int previewWidth = 640;
        private int previewHeight = 480;
        private AutoFitSurfaceView surfaceView;
        private SurfaceHolder surfaceHolder;

        public CameraStateCallback(V4L2Camera adCamera, AutoFitSurfaceView surfaceView, SurfaceHolder surfaceHolder){
            this.adCamera = adCamera;
            this.surfaceView = surfaceView;
            this.surfaceHolder = surfaceHolder;
        }
        @Override
        public void onOpened() {
            Log.d(TAG, "onOpened");

            Size chooseSize = this.adCamera.chooseOptimalSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
            if (chooseSize != null) {
                previewWidth = chooseSize.getWidth();
                previewHeight = chooseSize.getHeight();

            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    surfaceView.setAspectRatio(previewWidth, previewHeight);
                    Log.e(TAG, "setAspectRatio width:" + previewWidth + ", height:" + previewHeight);
                }
            });

            this.adCamera.setSurface(surfaceHolder);
            this.adCamera.startPreview(new CameraDataCallback());
        }

        @Override
        public void onError(int error) {

        }
    }

    class CameraDataCallback implements IDataCallback {

        @Override
        public void onDataCallback(byte[] data, int dataType, int width, int height) {
//            Log.e(TAG, "onDataCallbakck  dataType:" + dataType + ", width:" + width + ", height:" + height);
            //处理camera preview 数据
        }
    }
}
